package com.burgeon.backend.service.Impl;

import com.burgeon.backend.Rest.store.*;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.Store;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.StoreMapper;
import com.burgeon.backend.mappers.SwitchMapper;
import com.burgeon.backend.service.StoreService;
import com.burgeon.backend.service.ex.LoadingException;
import com.burgeon.backend.service.ex.UploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SwitchMapper switchMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public ArrayList<Store> GetStoreAll(String account) {
        Info info = GetInfo(account);
        GetStoreAll getStoreAll = new GetStoreAll(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            ArrayList<Store> data = getStoreAll.getData();

            return data;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("下拉菜单获取所有店仓错误");
            return null;
        }
    }

    @Override
    public HashMap GetInfo(String account, String id) {
        HashMap data = new HashMap();
        Info info = GetInfo(account);
        GetStoreMap getStoreMap = new GetStoreMap(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            data.put("map", getStoreMap.getData(id));
            data.put("account", account);
        } catch (Exception e) {
            System.err.println("获取门店map错误");
        }

        return data;
    }

    @Override
    public void UploadData(MultipartFile file, String account, String store, String id, String map) throws UploadException {

        System.err.println(store);
        System.err.println(id);
        String ImgPath = null;
        if (file!=null&&!file.isEmpty()) {

            String fileName = file.getOriginalFilename(); // 文件名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
            String filePath = "//root//code//img//" + account + "//"; // 上传后的路径

            fileName = store + suffixName; // 新文件名
            ImgPath = "http://lfwdcdn.cloudsmicro.com/img/"+account +"/"+ fileName;
            File dest = new File(filePath + fileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);

            } catch (IOException e) {
                e.printStackTrace();
                throw new UploadException("系统错误");
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

        }

        try {
            SyncMap(account, id, map,ImgPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void SyncMap(String account, String id, String map,String img) throws Exception {

        Info info = GetInfo(account);
        ModStore modStore = new ModStore(info.getUrl(), info.getAccount(), info.getPassword());
        modStore.getData(id, map,img);
    }

    @Override
    public void SyncStore(String account) throws LoadingException {
        Info info = GetInfo(account);
        String token = accountMapper.GetTokenForAccount(account);
        //从bos获取user数据
        SYNStoreData getStoreData = new SYNStoreData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            ArrayList<Store> data = getStoreData.getData();
            storeMapper.DelStoreData(account);
            for (Store store : data) {
                store.setAccount(account);
                store.setToken(token);
            }
            storeMapper.UpStoreData(data);

        } catch (Exception e) {

            System.err.println("同步错误");
            e.printStackTrace();
            throw new LoadingException("系统错误");
        }
    }

    @Override
    public HashMap GetStoreData(String account, String start) throws LoadingException {
        Info info = GetInfo(account);
        //从bos获取user数据
        GetStoreData getStoreData = new GetStoreData(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data;

        try {
            data = getStoreData.getData(Integer.valueOf(start));
        } catch (Exception e) {
            e.printStackTrace();
            throw new LoadingException("数据加载错误");
        }
        return data;
    }

    @Override
    public HashMap FindStoreData(String account, String start, String message) throws LoadingException {
        Info info = GetInfo(account);
        //从bos获取user数据
        FindStoreData findStoreData = new FindStoreData(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data;

        try {
            data = findStoreData.getData(Integer.valueOf(start), message);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LoadingException("数据加载错误");
        }
        return data;
    }

    @Override
    public List GetStore(String token, String city) {
        Info info = GetInfoByToken(token);
        String account = accountMapper.GetAccountForToken(token);
        GetStoreByCityForUser getStoreByCity = new GetStoreByCityForUser(info.getUrl(), info.getAccount(), info.getPassword());
        List data = new ArrayList();

        try {
            data = getStoreByCity.getData(city, account);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    @Override
    public HashMap GetStoreByCity(String city, String token) {
        Info info = GetInfoByToken(token);
        GetStoreByCity getStoreByCity = new GetStoreByCity(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data = new HashMap();
        String account = accountMapper.GetAccountForToken(token);
        try {
            data = getStoreByCity.getData(city, account);
            //开关
            data.put("empSwitch", switchMapper.GetSwitchByToken(token).getEmp());
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.err.println("会员获取门店信息失败");
        }
        return data;
    }

    private Info GetInfo(String account) {
        return accountMapper.GetAccData(account);
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }
}
